<?php
// @formatter:off
/**
 * @file WCOptions.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-22 17:58:11 +0000 (Sat, 22 Jun 2013) $
 *
 * $Id: WCOptions.php 83 2013-06-22 17:58:11Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA2/includes/WCOptions.php $
 */
// @formatter:on

/**
 * @class WCOptions
 */
class WCOption {
	protected $_activated = false;
	protected $_name = false;
	protected $_triggers = array();
	protected $_needValue = false;
	protected $_value = false;
	//
	// Public methods.
	public function activated() {
		return $this->_activated;
	}
	public function addTrigger($trigger) {
		if(strlen($trigger) > 1) {
			$trigger = "--{$trigger}";
		} else {
			$trigger = "-{$trigger}";
		}

		if(!in_array($trigger, $this->_triggers)) {
			$this->_triggers[] = $trigger;
		}
	}
	public function check($params = null) {
		global $argv;

		$this->_activated = false;

		$params = array_merge($argv, $params !== null ? $params : array());
		$needmore = false;
		foreach($params as $arg) {
			if($needmore) {
				$this->_value = $arg;
				$this->_activated = true;

				$needmore = false;
			} elseif(in_array($arg, $this->_triggers)) {
				if($this->needValue()) {
					$this->_activated = false;
					$this->_value = false;

					$needmore = true;
				} else {
					$this->_value = true;
					$this->_activated = true;
				}
			}
		}

		if($needmore) {
			trigger_error(__CLASS__."::".__FUNCTION__.": Option '{$this->name()}' needs a value (triggers: '".implode("', '", $this->_triggers)."').", E_USER_WARNING);
		}
	}
	public function name() {
		return $this->_name;
	}
	public function needValue() {
		return $this->_needValue;
	}
	public function setName($name) {
		$this->_name = $name;
		return $this->name();
	}
	public function setNeedValue($needs = true) {
		$this->_needValue = $needs;
		return $this->needValue();
	}
	public function value() {
		if($this->activated() && $this->needValue()) {
			return $this->_value;
		} else {
			return false;
		}
	}
};

/**
 * @class WCOptions
 */
class WCOptions {
	/**
	 * @var WCOptions This is the sole instance for this singleton class.
	 */
	protected static $_Instance;

	protected $_options = array();
	protected $_recheck = true;

	protected function __construct() {
	}
	public function __destruct() {
	}
	/**
	 * Prevent users from clone the singleton's instance.
	 */
	public function __clone() {
		trigger_error(__CLASS__.": Clone is not allowed.", E_USER_ERROR);
	}
	//
	// Public methods.
	public function activated($name) {
		return isset($this->_options[$name]) && $this->_options[$name]->activated();
	}
	public function addOption($name) {
		$out = false;

		if(!isset($this->_options[$name])) {
			$this->_options[$name] = new WCOption();
			$this->_options[$name]->setName($name);
			$this->_recheck = true;
			$out = true;
		}

		return $out;
	}
	public function addValueOption($name) {
		$out = false;

		if($this->addOption($name)) {
			$this->_options[$name]->setNeedValue(true);
			$out = true;
		}

		return $out;
	}
	public function addOptionTrigger($name, $trigger) {
		if(isset($this->_options[$name])) {
			$this->_options[$name]->addTrigger($trigger);
			$this->_recheck = true;
		}
	}
	public function check($params = null) {
		$this->recheck($params);
	}
	public function clear() {
		foreach($this->_options as $name => $opt) {
			unset($this->_options[$name]);
		}
		$this->_options = array();
	}
	public function needValue($name) {
		return isset($this->_options[$name]) && $this->_options[$name]->needValue();
	}
	public function option($name) {
		if(isset($this->_options[$name])) {
			return $this->_options[$name];
		} else {
			return false;
		}
	}
	public function value($name) {
		if(isset($this->_options[$name])) {
			return $this->_options[$name]->value();
		} else {
			return false;
		}
	}
	//
	// Protected methods.
	protected function recheck($params = null) {
		$check = $this->_recheck || $params !== null;
		if($check) {
			foreach($this->_options as $name => $opt) {
				$this->_options[$name]->check($params);
			}
		}
	}
	//
	// Public class methods.
	/**
	 * This class-method creates and returns the sole instance for this
	 * singleton class.
	 * @return WCOptions Sigleton instance.
	 */
	public static function Instance() {
		if(!isset(self::$_Instance)) {
			$c = __CLASS__;
			self::$_Instance = new $c();
		}

		return self::$_Instance;
	}
}
?>